package factorytest.iflytek.com.einkiflytekfactorytest.config;

import android.content.Context;
import android.content.SharedPreferences;

import factorytest.iflytek.com.einkiflytekfactorytest.agingtest.AgingTestItemAttr;
import factorytest.iflytek.com.einkiflytekfactorytest.factorytestui.FactoryTestItemsEnum;

/**
 * Created by sst on 2018/7/17.
 */

public final class PCCommand {

    /**
     * 清楚emmc测试失败的那个标志<p>
     * 使用方法:<p>
     * adb shell am broadcast -a pc_action.ft.clear_test_flag
     */
    public final static String PC_ACTION_CLEAR_EMMC_TEST_FLAG = "pc_action.ft.clear_test_flag";
    /**
     * 设置老化测试失败<p>
     * 使用方法:<p>
     *     adb shell am broadcast -a pc_action.ft.set_aging_failed
     *
     */
    public final static String PC_ACTION_SET_AGING_FAILED = "pc_action.ft.set_aging_failed";

    /**
     * 设置某个测试项强行over<p>
     * 使用方法:<p>
     *     <p>adb shell am broadcast -a pc_action.ft.set_aging_item_force_test_over --ei item_id 1</p>
     *     <p>adb shell am broadcast -a pc_action.ft.clr_aging_item_force_test_over --ei item_id 1</p>
     *     item_id : {@link AgingTestItemAttr#getId()}
     *
     */
    public final static String PC_ACTION_SET_AGING_ITEM_FORCE_TEST_OVER = "pc_action.ft.set_aging_item_force_test_over";
    public final static String PC_ACTION_CLR_AGING_ITEM_FORCE_TEST_OVER = "pc_action.ft.clr_aging_item_force_test_over";
    public final static String PC_ACTION_AGING_ITEM_INT_EXTRA = "item_id";

    /**
     * 设置某个测试项在默认测试次数或者时间下over的ratio, 比如原来是72次, 设ratio = 2.0f, 那么就需要测试144次才能over<p>
     * 使用方法:<p>
     *     <p>adb shell am broadcast -a pc_action.ft.set_aging_item_test_over_ratio --ei item_id 1 --ef ratio 1.5</p>
     *     item_id : {@link AgingTestItemAttr#getId()}
     *
     */
    public final static String PC_ACTION_SET_AGING_ITEM_TEST_OVER_RATIO = "pc_action.ft.set_aging_item_test_over_ratio";
    public final static String PC_ACTION_AGING_ITEM_RATIO_FLOAT_EXTRA = "ratio";

    /**
     * 设置所有测试都通过, 包括老化之后的测试项目.<p>
     * 使用方法:<p>
     *     adb shell am broadcast -a pc_action.ft.set_all_passed
     */
    public final static String PC_ACTION_SET_ALL_PASSED = "pc_action.ft.set_all_passed";

    /**
     * 设置所有测试都Fail, 包括老化之后的测试项目.<p>
     * 使用方法:<p>
     *     adb shell am broadcast -a pc_action.ft.set_all_failed
     */
    public final static String PC_ACTION_SET_ALL_FAILED = "pc_action.ft.set_all_failed";


    /**
     * 设置某一个测试项PASS,包括老化后的该测试项.<p>
     *
     * item_id {@link FactoryTestItemsEnum#getIdentify()} <p>
     * <p> first(1000)
     * <p> second(1001)
     * <p> third(1002)
     * <p> bluetooth(1003)
     * <p> aging(1004)
     * <p> wifi(1005)
     * <p> battery(1006)
     * <p> pcba(1007),
     * <p> sdcard(1008),
     * <p> ram(1009),
     * <p> rom(1010),
     *  <p>
     *  使用方法:<p>
     *  adb shell am broadcast -a pc_action.ft.set_item_passed --ei item_id 1003
     */
    public final static String PC_ACTION_SET_ITEM_PASSED = "pc_action.ft.set_item_passed";
    public final static String PC_ACTION_SET_ITEM_PASSED_INT_EXTRA = "item_id";

    /**
     * 设置某一个测试项FAIL,包括老化后的该测试项.<p>
     * item_id {@link FactoryTestItemsEnum#getIdentify()} <p>
     * <p> first(1000)
     * <p> second(1001)
     * <p> third(1002)
     * <p> bluetooth(1003)
     * <p> aging(1004)
     * <p> wifi(1005)
     * <p> battery(1006)
     * <p> pcba(1007),
     * <p> sdcard(1008),
     * <p> ram(1009),
     * <p> rom(1010),
     *  <p>
     *  使用方法:<p>
     *      adb shell am broadcast -a pc_action.ft.set_item_failed --ei item_id 1
     */
    public final static String PC_ACTION_SET_ITEM_FAILED = "pc_action.ft.set_item_failed";
    public final static String PC_ACTION_SET_ITEM_FAILED_INT_EXTRA = "item_id";


    /**
     * 停止老化测试<p>
     * 老化测试是靠时间来进行重启,休眠,硬件等测试项目, 如果时间出错, 一切都乱了.
     * 老化根本就停不下来了. 所以靠这个方法来手动停止老化测试.<p>
     *  使用方法:<p>
     *      adb shell am broadcast -a pc_action.ft.disable_aging
     */
    public final static String PC_ACTION_SET_AGING_DISABLED = "pc_action.ft.disable_aging";

    /**
     * 显示nvrom里的数据<p>
     *  使用方法:<p>
     *      adb shell am broadcast -a pc_action.ft.show_nvrom_data
     *      数据保存在 /sdcard/iflytek_ft/nvrom.log
     */
    public final static String PC_ACTION_SHOW_NVROM_DATA = "pc_action.ft.show_nvrom_data";

    ////////////////////////////////////////
    ////////////////////////////////////////
    private static final String PC_CMD_SHARED_PREF_NAME = "pc_cmd_shared_pref_name";
    private static final String PC_CMD_BROADCAST = "broadcast";

    public static boolean isFromPCBroadcast(Context context) {
        return 1 == getInt(context, PC_CMD_BROADCAST, 0);
    }
    public static void setFromPCBroadcast(Context context) {
        setInt(context, PC_CMD_BROADCAST, 1);
    }
    public static void clearFromPCBroadcast(Context context) {
        setInt(context, PC_CMD_BROADCAST, 0);
    }

    private static int getInt(Context context, String name, int defValue) {
        if (context == null) {
            return 0;
        }

        SharedPreferences sp = context.getSharedPreferences(PC_CMD_SHARED_PREF_NAME, Context.MODE_PRIVATE);

        return sp.getInt(name, defValue);
    }

    private static void setInt(Context context, String name, int value) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(PC_CMD_SHARED_PREF_NAME, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sp.edit();

        editor.putInt(name, value);

        editor.apply();
    }
}
